package com.vecspace.dayu.code.ui.pojo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vecspace.data.model.IdEntity;
import com.vecspace.data.model.RowMapper;
import com.vecspace.dayu.code.template.pojo.IPageComponent;

import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Polymorphism;
import org.hibernate.annotations.PolymorphismType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/** DAYU-表单
 * @author wanghua
 */
@Entity
@Polymorphism(type = PolymorphismType.EXPLICIT)
@Table(name = "DAYU_CODE_UI_FORM")
public class Form  implements IPageComponent, IdEntity, java.io.Serializable {
	
	private static final long serialVersionUID = 4645215006909658137L;
	public Form(){
	}
	
	private Long id;
	/**逻辑ID*/
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "ID")
	public Long getId(){
		return id;
	}
	/**逻辑ID*/
	public void setId(Long value){
		this.id = value;
	}

	protected Long tableId;
	/**数据表ID*/
	@Column(name = "TABLE_ID")
	public Long getTableId(){
		return tableId;
	}
	/**数据表ID*/
	public void setTableId(Long value){
		this.tableId = value;
	}

	private String platformType;//终端类型
	@Column(name = "PLATFORM_TYPE")
	public String getPlatformType() {
		return platformType;
	}
	public void setPlatformType(String platformType) {
		this.platformType = platformType;
	}

	private Integer columnCount;
	/**表单列数*/
	@Column(name = "COLUMN_COUNT")
	public Integer getColumnCount(){
		return columnCount;
	}
	/**表单列数*/
	public void setColumnCount(Integer value){
		this.columnCount = value;
	}

	private Integer ordnum;
	/**顺序号*/
	@Column(name = "ORDNUM")
	public Integer getOrdnum(){
		return ordnum;
	}
	/**顺序号*/
	public void setOrdnum(Integer value){
		this.ordnum = value;
	}

	private Date createTime;
	/**记录创建时间*/
	@JsonIgnore(true)
	@Column(name = "CREATE_TIME")
	public Date getCreateTime(){
		return createTime;
	}
	/**记录创建时间*/
	public void setCreateTime(Date value){
		this.createTime = value;
	}
	@Transient
	@JsonIgnore(true)
	public String getCreateTimeStr(){
		if(createTime == null){
			return "";
		}
		String exp = "yyyy-MM-dd";
		String timestr = new java.text.SimpleDateFormat(exp).format(createTime);
		return timestr;
	}

	private String createUser;
	/**记录创建人ID*/
	@Column(name = "CREATE_USER")
	public String getCreateUser(){
		return createUser;
	}
	/**记录创建人ID*/
	public void setCreateUser(String value){
		this.createUser = value;
	}

	
	public void parseFrom(Map<String, Object> row){
		RowMapper rowMapper = new RowMapper(row);
		this.id = rowMapper.getLong("ID");
		this.platformType = rowMapper.getString("PLATFORM_TYPE");
		this.tableId = rowMapper.getLong("TABLE_ID");
		this.columnCount = rowMapper.getInteger("COLUMN_COUNT");
		this.ordnum = rowMapper.getInteger("ORDNUM");
		this.createTime = rowMapper.getDate("CREATE_TIME");
		this.createUser = rowMapper.getString("CREATE_USER");
	}
	private List<FormField> formFields;

	@JsonIgnore
	@Transient
	public List<FormField> getFormFields() {
		return formFields;
	}
	public void setFormFields(List<FormField> formFields) {
		this.formFields = formFields;
	}
	
	/**在fields的第一个位置插入一个空的Field（用于在下拉列表中显示一个空项）
	 * 
	 *  
	 * @author wanghua
	 * @reviewer  
	 * @createTime 2021年9月16日 下午5:41:34
	 */
	public void addEmptyField() {
		if(formFields != null) {
			formFields.add(0, new FormField());
		}
	}

	@Transient
	public int getFormColumnCount(){
		if(this.columnCount == null || this.columnCount <= 0){
			return 1;
		}
		else {
			return this.columnCount;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null)return false;
		if(id == null)return false;
		Form other = (Form)obj;
		return id.equals(other.id);
	}

	@Override
	public int hashCode() {
		if(id == null)return 0;
		return id.hashCode();
	}
}
